package cn.whdream.datastructure.stack;

import java.util.Scanner;
import java.util.Stack;

import javax.management.RuntimeErrorException;

/**
 * 
 * @author admin
 * 数组栈
 */
public class ArrayStack {
	private int top = -1;
	private int maxSize;
	private int[] stack;
	public ArrayStack(int maxSize) {
		super();
		this.maxSize = maxSize;
		stack = new int[maxSize];
	}
	
	public boolean isFull() {
		return top == maxSize - 1;
	}
	
	public boolean isEmpty() {
		return top < 0;
	}
	
	public void add(int num) {
		if (isFull()) {
			System.out.println("栈满了");
		}
		top++;
		stack[top] = num;
	}
	public int pop() {
		if (isEmpty()) {
			throw new RuntimeException("栈为空");
		}
		int temp = stack[top];
		top--;
		return temp;
	}
	
	public void list() {
		if (isEmpty()) {
			System.out.println("栈为空");
			return;
		}
		for (int i = top; i >-1 ; i--) {
			System.out.println("num:"+stack[i]);
		}
	}
	
	public int peek() {
		if (isEmpty()) {
			return -1;
		}
		return stack[top];
	}
	public static void main(String[] args) {
		ArrayStack stack = new ArrayStack(4);
		boolean loop = true;
		Scanner scanner = new Scanner(System.in);
		while (loop) {
			System.out.println("请输入下列字母进行操作");
			char key = scanner.next().charAt(0);
			try {
				switch (key) {
				case 'a':
					System.out.println("请输入一个数字");
					int num = scanner.nextInt();
					stack.add(num);
					break;
				case 's':
					System.out.println("当前栈为：");
					stack.list();
					break;
				case 'g':
					System.out.printf("data: %d \n", stack.pop());
					break;
				case 'e':
					loop = false;
					break;
				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("程序结束");
	}
	
	
	
}
